<?php

namespace app\manage\controller;

use app\manage\model\AuthGroup as AuthGroupmodel;
use app\manage\controller\BaseController;
use think\facade\Db;
use think\facade\View;

class AuthGroup extends BaseController
{
    public function index()
    {
        $data = AuthGroupmodel::order('sort', 'ASC')->select();
        foreach ($data as &$v){
            $array= Db::name('pilot_list')->where('id','in',$v['rules'])->where('isopen', 1)->column('name');
            $v['nei']=implode(",", $array);
        }

        View::assign('data_arr', $data);
        $count1 = count($data);
        View::assign('count1', $count1);
        return View::fetch();
    }

    public function ajax()
    {
        $data = input('post.');
        if ($data['type'] == 'AuthGroup_start') {
            if (AuthGroupmodel::update(['isopen' => 1], ['id' => $data['id']])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type'] == 'AuthGroup_stop' and $data['id'] != 1) {
            if (AuthGroupmodel::update(['isopen' => 0], ['id' => $data['id']])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type'] == 'AuthGroup_del' and $data['id'] != 1) {
            if (AuthGroupmodel::destroy($data['id'])) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        if ($data['type'] == 'AuthGroup_sort') {
            $arrlength = count($data['id']);
            $ar = [];
            for ($x = 0; $x < $arrlength; $x++) {
                $ar[$x] = ['id' => $data['id'][$x], 'sort' => $data['sort'][$x]];
            }
            $info = new AuthGroupmodel();
            if ($info->saveAll($ar)) {
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
        }
        return json(['code' => 0, 'message' => '失败']);
    }
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $validate = new \app\manage\validate\AuthGroup;
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if (!isset($data['rules'])) {
                $this->error('请添加规则后提交！');
            }
            if ($data['rules']) {
                $data['rules'] = implode(',', $data['rules']);
            }
            if (AuthGroupmodel::create($data)) {
                $this->success('添加成功',url('auth_group/index',['st'=>1]));
            } else {
                $this->error('添加失败');
            }
        }

        $datasort = Db::name('pilot_list')->where('fid', 0)->where('isopen', 1)->order('sort asc,id desc')->select()->toArray();
        foreach ($datasort as &$v) {
            $v['zi'] = Db::name('pilot_list')->where('fid', $v['id'])->where('isopen', 1)->order('sort asc,id desc')->select();
        }
        View::assign('datasort', $datasort);
        return View::fetch();
    }

    public function edit()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $validate = new \app\manage\validate\AuthGroup;
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }
            if (!isset($data['rules'])) {
                $this->error('请添加规则后提交！');
            }
            if ($data['rules']) {
                $data['rules'] = implode(',', $data['rules']);
            }
            if (AuthGroupmodel::update($data, ['id'=>$data['id']])) {
                $this->success('修改成功',url('auth_group/index',['st'=>1]));
            } else {
                $this->error('修改失败');
            }
        }

        $cid = input('id');
        $data = Db::name('AuthGroup')->where('id', $cid)->find();
        View::assign('data', $data);
        $datasort = Db::name('pilot_list')->where('fid', 0)->where('isopen', 1)->order('sort asc,id desc')->select()->toArray();
        foreach ($datasort as &$v) {
            $v['zi'] = Db::name('pilot_list')->where('fid', $v['id'])->where('isopen', 1)->order('sort asc,id desc')->select();
        }
        View::assign('datasort', $datasort);
        View::assign('rules', explode(",", $data['rules']));
        return View::fetch();
    }
}
